package com.realbrand.net.realbrandpay.wxapi;


import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

import com.realbrand.net.realbrandpay.ui.activity.shopcar.CheckOutActivity;
import com.realbrand.net.realbrandpay.untils.Constant;
import com.realbrand.net.realbrandpay.zxing.util.L;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";

    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SharedPreferences sharedPreferences = getSharedPreferences(Constant.shareName, MODE_PRIVATE);
        String id = sharedPreferences.getString("app_id", "");
        api = WXAPIFactory.createWXAPI(this, id);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp baseResp) {

        L.d(L.TAG, "onResp+++++++++WXPayEntryActivity");
        if (baseResp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            L.d(L.TAG, "onResp" + baseResp.errCode);
            if (baseResp.errCode == 0) {
                Intent intent = new Intent(WXPayEntryActivity.this, CheckOutActivity.class);
                intent.putExtra(Constant.formWXPay, Constant.formWXPay);
                startActivity(intent);
                finish();
            } else {
                finish();
            }
        }
    }


}